package com.book.system.controller;

import com.book.breakage.entity.Accident;
import com.book.common.annotation.Log;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.FebsResponse;
import com.book.common.entity.QueryRequest;
import com.book.common.utils.FebsUtil;
import com.book.system.entity.User;
import com.book.system.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Zhang Gaohe
 * @Date: 2021/4/2
 **/
@Controller
public class SessionController extends BaseController {
    @Autowired
    IUserService userService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "session/online")
    public String breReturnIndex() {
        return FebsUtil.view("system/user/online");
    }

    @GetMapping("session/list")
    @ResponseBody
    @RequiresPermissions("session:list")
    public FebsResponse accidentList(HttpServletRequest request, User user) {
        List<User> list = userService.onlineList(request, user);
        Map<String, Object> dataTable = new HashMap<>();
        dataTable.put("rows", list);
        dataTable.put("total", list.size());
        return new FebsResponse().success().data(dataTable);
    }

    @RequestMapping("session/kickout/{ids}")
    @ResponseBody
    @RequiresPermissions("session:kickout")
    public FebsResponse forceLogout(@PathVariable String ids) {
        String[] id = ids.split(",");
        try {
            userService.forceLogout(id);
            return new FebsResponse().success();
        } catch (Exception e) {
            return new FebsResponse().message("踢出用户失败");
        }
    }
}
